package v1

import (
	"github.com/gin-gonic/gin"
	"manager/bll"
	"manager/model"
	"manager/server/web/middleware"
	"manager/utils"
)

var Area = &area{}

func init() {
	// 注册路由
	RegisterRouter(Area)
}

type area struct{}

func (a *area) Init(r *gin.RouterGroup) {
	g := r.Group("/area", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
	}
}

func (a *area) create(c *gin.Context) {
	var (
		in  = &model.AreaCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Area.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (a *area) update(c *gin.Context) {
	var (
		in  = &model.AreaUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Area.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (a *area) list(c *gin.Context) {
	var (
		in  = &model.AreaListRequest{}
		out = &model.AreaListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.Area.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

func (a *area) delete(c *gin.Context) {
	var (
		in  = &model.AreaDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Area.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
